package org.termine.jsf;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import org.termine.db.RedisConnector;
import org.termine.entity.Location;
import org.termine.entity.PersonalData;
import org.termine.logic.LocationManager;
import org.termine.logic.PersonalDataManager;
import org.termine.util.ConfigurationBean;

@ManagedBean
@SessionScoped
public class LoginJSF extends BaseJSF implements Serializable {

	private static final long serialVersionUID = 7485188261726765110L;

	protected boolean REDIS_ACTIVE; // true -> personalData, location, selectedLocation,
	                                                 // selectedService are stored in session

	@ManagedProperty(value = "#{locationManager}")
	private LocationManager locationManager;
	@ManagedProperty(value = "#{personalDataManager}")
	private PersonalDataManager personalDataManager;
	@ManagedProperty(value = "#{redisConnector}")
	private RedisConnector redisConnector;
	@ManagedProperty(value="#{configurationBean}")
	private ConfigurationBean configurationBean;

	private String email;
	private String password;
	private String whoAmI; // 'ANONYM' or 'CLIENT' or 'PROVIDER'; is being set
						   // in MenuJSF on context switch
	private String sessionKey;
	private PersonalData personalData;
	private Location location;

	/*
	 * Init
	 */
	@PostConstruct
	public void init() {
		this.whoAmI = "ANONYM";
		this.REDIS_ACTIVE = Boolean.parseBoolean(this.configurationBean.getProperty("use_redis"));
	}

	/*
	 * Jsf Action Methods
	 */
	public String performLogin() {
		this.sessionKey = this.email;
		this.setPersonalData(this.getPersonalDataManager().getPersonalData(this.email, this.password));
		if (this.getPersonalData() != null) {
			if (this.getPersonalData().isProvider()) {
				this.whoAmI = "PROVIDER";
			}
			if (this.getPersonalData().isClient()) {
				this.whoAmI = "CLIENT";
			}
			this.setLocation(this.getLocationManager().getLocation(this.getPersonalData().getLocation_id()));
			this.email = "";
			this.password = "";
			this.setInfoMessage("hi " + this.getPersonalData().getForename());
			return this.whoAmI;
		} else {
			this.email = "";
			this.password = "";
			this.setErrorMessage("login failed");
			return "ERROR";
		}
	}

	public String performLogout() {
		String forename = this.getPersonalData().getForename();

		// on logout all objects, stored in session, are deleted
		// and the redis connection is destroyed
		if (this.REDIS_ACTIVE) {
			this.redisConnector.del(this.sessionKey + "_location");
			this.redisConnector.del(this.sessionKey + "_personaldata");
			this.redisConnector.destroy();
		}

		this.init();
		this.setInfoMessage("bye " + forename);
		return "SUCCESS";
	}

	/*
	 * Session Variables
	 */
	public PersonalData getPersonalData() {
		if (this.REDIS_ACTIVE) {
			return (PersonalData) this.redisConnector.get(this.sessionKey + "_personaldata");
		} else {
			return this.personalData;
		}
	}

	public void setPersonalData(PersonalData personalData) {
		if (this.REDIS_ACTIVE) {
			this.redisConnector.setExp(this.sessionKey + "_personaldata", RedisConnector.toString(personalData));
		} else {
			this.personalData = personalData;
		}
	}

	public Location getLocation() {
		if (this.REDIS_ACTIVE) {
			return (Location) this.redisConnector.get(this.sessionKey + "_location");
		} else {
			return this.location;
		}
	}

	public void setLocation(Location location) {
		if (this.REDIS_ACTIVE) {
			this.redisConnector.setExp(this.sessionKey + "_location", RedisConnector.toString(location));
		} else {
			this.location = location;
		}
	}

	/*
	 * Getters & Setters
	 */
	public String getEmail() {
		return this.email;
	}

	public PersonalDataManager getPersonalDataManager() {
		return this.personalDataManager;
	}

	public void setPersonalDataManager(PersonalDataManager personalDataManager) {
		this.personalDataManager = personalDataManager;
	}

	public String getWhoAmI() {
		return this.whoAmI;
	}

	public void setWhoAmI(String whoAmI) {
		this.whoAmI = whoAmI;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public LocationManager getLocationManager() {
		return this.locationManager;
	}

	public void setLocationManager(LocationManager locationManager) {
		this.locationManager = locationManager;
	}

	public String getSessionKey() {
		return this.sessionKey;
	}

	public void setSessionKey(String sessionKey) {
		this.sessionKey = sessionKey;
	}

	public RedisConnector getRedisConnector() {
		return this.redisConnector;
	}

	public void setRedisConnector(RedisConnector redisConnector) {
		this.redisConnector = redisConnector;
	}

	public ConfigurationBean getConfigurationBean() {
		return this.configurationBean;
	}

	public void setConfigurationBean(ConfigurationBean configurationBean) {
		this.configurationBean = configurationBean;
	}

}
